﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessClasses;
namespace TicketsOnline
{
    public partial class BuyTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["event"] == null)
            {
                return;
            }
            DataAccessLayer dal = new DataAccessLayer();
            try
            { 
                ListViewSelectedEvent.DataSource = dal.Events.Where(x => (x.EventID == int.Parse(Request.QueryString["event"])));
                ListViewSelectedEvent.DataBind();
            }
            catch
            {

            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            DataAccessClasses.DataAccessLayer dal = new DataAccessClasses.DataAccessLayer();
            ResultLabel.ForeColor = System.Drawing.Color.Red;
 
            
            int eventId;
            if(Request.QueryString["event"]==null || !int.TryParse(Request.QueryString["event"],out eventId))
            {
                ResultLabel.Text = "Invalid event!";
                return;
            }

            int ticketCount;
            if(!int.TryParse(AmountBox.Text,out ticketCount))
            {
                ResultLabel.Text = "Invalid amount!";
                return;
            }

            Event ev = dal.Events.Where(x => (x.EventID == int.Parse(Request.QueryString["event"]))).SingleOrDefault();
            if (ev==null ||  ev.Date <= DateTime.Now.AddDays(3))
            {
                ResultLabel.Text = "Invalid event!";
                return;
            }

            if (NameBox.Text.Length == 0 || AddressBox.Text.Length == 0)
            {
                ResultLabel.Text = "Required field(s) are missing!";
                return;
            }

            int refNo = dal.CreatePurchase(eventId, NameBox.Text, AddressBox.Text, ticketCount);
            ResultLabel.ForeColor = System.Drawing.Color.Green;
            ResultLabel.Text = "Success: Your refference number is #" + refNo;
        }
    }
}